<template>
  <div>
    <div class="bg"></div>
    <el-container class="home_box">
      <el-header class="header">
        <div class="title">北航二手货交易平台</div>
        <el-input
          class="space"
          type="text"
          placeholder="搜索商品"
          v-model="search"
          @keydown.enter.native="searchpage"
          v-bind:class="{ visible: this.$route.path === '/search' }"
        ></el-input>
        <div
          class="search"
          v-bind:class="{ visible: this.$route.path === '/search' }"
        >
          <el-button type="primary" icon="el-icon-search" @click="searchpage"
            >搜索</el-button
          >
        </div>
        <div class="mainpage">
          <router-link to="mainpage">主页</router-link>
        </div>
        <div class="mainpage">
          <router-link to="myinfo">个人中心</router-link>
        </div>
        <div class="notlogin" v-if="this.$store.state.isLogin === false">
          <ul>
            <li>
              <el-button type="primary" round @click="login">登录</el-button>
            </li>
            <li>
              <el-button type="primary" round @click="regest">注册</el-button>
            </li>
          </ul>
        </div>
        <div class="username" v-else>
          <el-avatar :size="50" fit="fill" :src="userimg"></el-avatar>
          <div class="user">
            你好,&nbsp;<el-dropdown @command="handleCommand">
              <span class="el-dropdown-link" @click="userpage"
                >{{ getusername()
                }}<i class="el-icon-arrow-down el-icon--right"></i
              ></span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="userpage">个人中心</el-dropdown-item>
                <el-dropdown-item command="myorder">我的订单</el-dropdown-item>
                <el-dropdown-item command="mypublish"
                  >我的发布</el-dropdown-item
                >
                <el-dropdown-item command="mymessage"
                  >我的消息</el-dropdown-item
                >
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </el-header>
    </el-container>
    <div class="xuanfu">
      <div class="bar_btn" @click="publish">
        <i class="el-icon-circle-plus-outline"></i>发布商品
      </div>
      <div class="bar_btn" @click="request">
        <i class="el-icon-circle-plus-outline"></i>发布需求
      </div>
      <div class="bar_btn" @click="myorder">
        <i class="el-icon-s-order"></i>我的订单
      </div>
      <div class="bar_btn" @click="mymessage">
        <i class="el-icon-message"></i>我的消息
      </div>
      <div class="bar_btn" @click="mycollect">
        <i class="el-icon-shopping-bag-1"></i>我的收藏
      </div>
    </div>
    <el-dialog title="新建发布" :visible.sync="addPublish">
      <el-form :model="publishForm">
        <el-form-item label="商品"
          ><el-input v-model="publishForm.name"></el-input
        ></el-form-item>
        <el-form-item>
          <el-upload
            accept="image/jpeg,image/jpg,image/png,image/svg"
            :limit="1"
            :action="''"
            :with-credentials="true"
            :http-request="upimgpublish"
          >
            <el-button size="small" type="primary"
              >点击上传图片</el-button
            ></el-upload
          >
        </el-form-item>
        <el-form-item label="商品分类"
          ><el-select
            v-model="publishForm.catalog"
            placeholder="请选择商品类别"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option></el-select
        ></el-form-item>
        <el-form-item label="商品价格"
          ><el-input v-model="publishForm.price"></el-input
        ></el-form-item>
        <el-form-item label="商品数量">
          <el-input-number v-model="publishForm.num" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="商品简介"
          ><el-input
            v-model="publishForm.introduction"
            type="textarea"
          ></el-input
        ></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addPublish = false">取 消</el-button>
        <el-button type="primary" @click="confirmPublish">确定发布</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新建需求" :visible.sync="addRequest">
      <el-form :model="requestForm">
        <el-form-item label="需求商品"
          ><el-input v-model="requestForm.name"></el-input
        ></el-form-item>
        <!-- <el-form-item>
          <el-upload
            accept="image/jpeg,image/jpg,image/png,image/svg"
            :limit="1"
            :action="''"
            :with-credentials="true"
            :http-request="upimgrequest"
          >
            <el-button size="small" type="primary"
              >点击上传图片</el-button
            ></el-upload
          >
        </el-form-item> -->
        <el-form-item label="商品分类"
          ><el-select
            v-model="requestForm.catalog"
            placeholder="请选择商品类别"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option></el-select
        ></el-form-item>
        <el-form-item label="期望价格"
          ><el-input v-model="requestForm.price"></el-input
        ></el-form-item>
        <el-form-item label="需求数量">
          <el-input-number v-model="requestForm.num" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="需求描述"
          ><el-input
            v-model="requestForm.introduction"
            type="textarea"
          ></el-input
        ></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addRequest = false">取 消</el-button>
        <el-button type="primary" @click="confirmRequest">确定发布</el-button>
      </div>
    </el-dialog>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  inject: ["reload"],
  data() {
    return {
      userimg: sessionStorage.getItem("userimg"),
      addPublish: false,
      addRequest: false,
      addFriend: {
        username: "老八",
        userid: "123456",
      },
      publishForm: {
        name: "",
        imgurl: "",
        catalog: "",
        price: "",
        num: "1",
        introduction: "",
      },
      requestForm: {
        name: "",
        imgurl: "",
        catalog: "",
        price: "",
        num: "1",
        introduction: "",
      },
      options: [
        { label: "教辅资料", value: 2 },
        { label: "生活用品", value: 3 },
        { label: "电子产品", value: 4 },
        { label: "网络资源", value: 5 },
        { label: "其他", value: 6 },
      ],
      activeIndex: "1",
      search: "",
    };
  },
  created() {
    this.$store.commit("initLog");
  },
  methods: {
    upimgpublish(item) {
      const { file } = item;
      const formdata = new FormData();
      formdata.append("file", file);
      this.$http.post("upload/img/message", formdata).then((res) => {
        if (res.data.success === true) {
          this.$message.success("上传成功!");
          this.publishForm.imgurl =
            "http://8.140.135.252:8080" + res.data.imgUrl;
        } else this.$message.error("上传失败,请稍后重新上传!");
      });
    },
    upimgrequest(item) {
      const { file } = item;
      const formdata = new FormData();
      formdata.append("file", file);
      this.$http.post("upload/img/message", formdata).then((res) => {
        if (res.data.success === true) {
          this.$message.success("上传成功!");
          this.requestForm.imgurl =
            "http://8.140.135.252:8080" + res.data.imgUrl;
        } else this.$message.error("上传失败,请稍后重新上传!");
      });
    },
    publish() {
      if (this.$store.state.isLogin === false) {
        alert("请先登录!");
        return;
      }
      this.addPublish = true;
    },
    request() {
      if (this.$store.state.isLogin === false) {
        alert("请先登录!");
        return;
      }
      this.addRequest = true;
    },
    myorder() {
      this.$router.push("myorder");
    },
    mymessage() {
      this.$router.push("mymessage");
    },
    mycollect() {
      this.$router.push("mycollect");
    },
    confirmPublish() {
      this.addPublish = false;
      //上传
      var good = {
        name: this.publishForm.name,
        imgDir: this.publishForm.imgurl,
        goodsDesc: this.publishForm.introduction,
        price: this.publishForm.price,
        inventory: this.publishForm.num,
        cat_id: this.publishForm.catalog,
      };
      this.$http.post("addGoods", good).then((res) => {
        if (res.data.success === true) {
          this.$message.success("添加成功!");
          this.add = false;
        } else {
          this.$message.error("添加失败,请稍后再试!");
        }
      });
    },
    confirmRequest() {
      this.addRequest = false;
      //上传
      var request = {
        title: this.requestForm.name,
        cat_id: this.requestForm.catalog,
        num: this.requestForm.num,
        req_desc: this.requestForm.introduction,
        pref_price: this.requestForm.price,
        time_limit: "3", //>>>>>>>>>>>>>>>>>>>>.
      };
      this.$http.post("addRequest", request).then((res) => {
        if (res.data.success === true) {
          this.$message.success("添加成功!");
          this.add = false;
        } else {
          this.$message.error("添加失败,请稍后再试!");
        }
      });
    },
    popwindow(user) {
      var msg = "来自 " + user.username + " 的好友申请";
      var n = 0;
      this.$notify({
        title: msg,
        message:
          "<div style='user-select:none'>点击标签接受好友申请,关闭标签忽略好友申请</div>",
        position: "bottom-right",
        onClick() {
          n = n + 1;
          this.$notify.closeAll();
        },
        onClose() {
          n = n + 1;
          if (n === 1) alert("reject");
          else alert("accept");
        },
        dangerouslyUseHTMLString: true,
        duration: 0,
      });
    },
    searchpage() {
      if (this.search === "") {
        this.$message({ duration: 500, message: "请输入搜索内容..." });
        return;
      }
      //搜索search
      this.$store.commit("setSearch", this.search);
      this.search = "";
      if (this.$route.path !== "/search") this.$router.push("search");
    },
    handleCommand(command) {
      if (command === "logout") this.logout();
      if (command === "userpage") this.userpage();
      if (command === "myorder") this.myorder();
      if (command === "mymessage") this.mymessage();
      if (command === "mypublish") this.mypublish();
    },
    logout() {
      this.$router.push("mainpage");
      this.$http.post("/logout");
      window.sessionStorage.clear();
      this.$store.commit("logout");
      this.$message("用户退出!");
      this.reload;
    },
    login() {
      this.$router.push("login");
    },
    regest() {
      this.$router.push("regest");
    },
    getusername() {
      return window.sessionStorage.getItem("username");
    },
    userpage() {
      this.$router.push("personal_center");
    },
    myorder() {
      this.$router.push("myorder");
    },
    mymessage() {
      this.$router.push("mymessage");
    },
    mypublish() {
      this.$router.push("mypublish");
    },
  },
};
</script>


<style lang="less" scoped>
.xuanfu {
  width: 150px;
  position: fixed;
  right: 60px;
  top: 500px;
  margin-top: -190px;
  z-index: 1100;
}
.bar_btn {
  width: 100%;
  height: 60px;
  font-size: 25px;
  line-height: 60px;
  border: 2px solid #72787e;
  margin-bottom: 10px;
  text-align: center;
  border-radius: 30px;
  user-select: none;
  background-color: #fff;
}
.bar_btn:hover {
  color: #409eff;
  border: 2px solid #409eff;
}
/deep/.el-dialog__title {
  line-height: 30px;
  font-size: 30px;
}
/deep/.el-form-item__label {
  font-size: 20px;
}
/deep/.el-input__inner {
  font-size: 18px;
}
/deep/.el-textarea__inner {
  font-size: 18px;
}
/deep/.el-dialog .el-button {
  font-size: 19px;
}
.bg {
  background-image: url(../img/bg.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -10;
  width: 100%;
  height: 100%;
}
.header {
  background-color: #f0f0f0;
  height: 80px !important;
  // position: fixed;
}
.title {
  display: block;
  float: left;
  font-size: 40px;
  font-weight: 600;
  font-family: "SimSun";
  line-height: 80px;
}
.space {
  display: block;
  float: left;
  color: #f0f0f0;
  width: 500px;
  line-height: 80px;
  padding: 0 10px 0 80px;
}
.search {
  display: block;
  float: left;
  line-height: 80px;
  padding-right: 160px;
}
.mainpage {
  display: block;
  float: left;
  font-size: 27px;
  font-weight: 400;
  line-height: 80px;
  height: 100%;
  width: 120px;
}
a {
  display: block;
  height: 98%;
  text-align: center;
  text-decoration: none;
  color: #000;
}
a:hover {
  color: #3a79b8;
  border-bottom: 4px solid #3a79b8;
}
.home_box {
  height: 100%;
}
.notlogin,
.item {
  display: block;
  float: right;
  line-height: 80px;
}
.header ul {
  display: inline-block;
  padding: 0;
  margin: 0;
}
.header ul li {
  display: inline-block;
  list-style: none;
  padding-left: 10px;
}
.username {
  display: block;
  float: right;
  height: 100%;
  font-size: 18px;
  line-height: 80px;
  width: 205px;
}
.username .el-button {
  font-size: 18px !important;
}
/deep/.el-dropdown,
.el-dropdown-menu {
  font-size: 18px !important;
}
/deep/.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
/deep/.el-icon-arrow-down {
  font-size: 12px;
}
/deep/.el-avatar {
  margin-top: 15px;
}
.user {
  display: flex;
  float: right;
  line-height: 80px;
  margin-right: 30px;
}
.visible {
  visibility: hidden;
}
</style>